package com.ebt.m.customer.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.commons.buscomponent.listview.BaseListView;
import com.ebt.m.commons.buscomponent.listview.ISectionFactory;
import com.ebt.m.commons.buscomponent.listview.IView;
import com.ebt.m.commons.buscomponent.listview.SectionItem;
import com.ebt.m.customer.view.CustomerItemCharTitle;
import com.ebt.m.customer.view.CustomerItemPolicy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Damon on 2017/11/17.
 * 保单客户listview
 */

public class ListViewPolicyCustomer extends BaseListView<CustomerPolicyPresenter> {

    public static final int DATA_TYPE_CHAR_TITLE = 1;
    public static final int DATA_TYPE_CUSTOMER = 2;

    private int from;

    public ListViewPolicyCustomer(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ListViewPolicyCustomer(Context context) {
        this(context, null);
    }

    public ListViewPolicyCustomer(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mRefreshLayout.setRefreshNeeded(true);
    }

    @Override
    protected ISectionFactory createViewFactory() {
        return new ISectionFactory() {
            @Override
            public IView createView(int viewType) {
                if (viewType == DATA_TYPE_CHAR_TITLE) {
                    CustomerItemCharTitle customerItemCharTitle = new CustomerItemCharTitle(getContext());
                    return customerItemCharTitle;
                } else {
                    CustomerItemPolicy itemView = new CustomerItemPolicy(getContext());
                    itemView.setFrom(from);
                    return itemView;
                }
            }
        };
    }


    @Override
    protected CustomerPolicyPresenter createPresenter() {
        return new CustomerPolicyPresenter(getContext(), this);
    }

    @Override
    public void loadNext() {

    }

    @Override
    public void update(Object... args) {
        super.update(args);
        from = (int) args[0];
        mPresenter.loadNew(args);

    }

    @Override
    protected boolean createFootView() {
        return false;
    }

    @Override
    protected View configEmptyView() {
        View emptyContent = LayoutInflater.from(getContext()).inflate(R.layout.common_list_empty_view, null);
        TextView textView = (TextView) emptyContent.findViewById(R.id.tv_empty);
        ImageView img = (ImageView) emptyContent.findViewById(R.id.iv_empty);
        textView.setText("暂无保单客户");
        img.setImageResource(R.drawable.ic_no_customer);
        return emptyContent;
    }

    public void toMovePositon(String charStr) {
        int readyToPosition = -1;
        if ("#".equalsIgnoreCase(charStr)) {
            readyToPosition = 0;
        }
        List<SectionItem> sectionItems = mPresenter.getItems();
        SectionItem sectionItem = null;
        HashMap<Integer, Integer> charAndPosition = new HashMap<>();
        List<Integer> listCharInt = new ArrayList<>();
        for (int i = 0; i < sectionItems.size(); i++) {
            sectionItem = sectionItems.get(i);
            if (sectionItem.data instanceof String) {
                String string = (String) sectionItem.data;
                Integer charInt = (int) string.charAt(0);
                charAndPosition.put(charInt, i);
                listCharInt.add(charInt);
            }
        }
        //判断当前传入char是否大于实际char
        int charInt = (int) charStr.charAt(0);
        if (listCharInt.size() > 0 && charInt <= listCharInt.get(0)) {
            readyToPosition = 0;
        } else if (listCharInt.size() > 0 && charInt >= listCharInt.get(listCharInt.size() - 1)) {
            readyToPosition = sectionItems.size() - 1;
        } else {
            for (Integer key : charAndPosition.keySet()) {
                if (charInt == key) {
                    readyToPosition = charAndPosition.get(key);
                }
            }
        }
        mRecyclerView.scrollToPosition(readyToPosition);
    }

}
